import { ref } from 'vue'
import { defineStore } from 'pinia'

export const useCoinStore = defineStore('coin', () => {
    const coinInfo = ref({
        name: 'BTC',
        price: '',
        inCharts: false,
        kLineType: '15分钟',
        buyPrice: '',
        sellPrice: ''
    })
    const spotKLine = ref<Record<string, any>>({
    })
    const contractKLine = ref<Record<string, any>>({
    })
    const setContractKLine = (name: any, kLine: any) => {
        contractKLine.value[name] = kLine
    }
    const setSpotKLine = (name: any, kLine: any) => {
        spotKLine.value[name] = kLine
    }
    const coinPriceList = ref<{ coinName: string;[key: string]: any }[]>([])
    const setCoinPriceList = (list: { coinName: string;[key: string]: any }[]) => {
        coinPriceList.value = list
    }
    const setPirce = (type: string, price: string) => {
        if (type === 'buy') {
            coinInfo.value.buyPrice = price
        } else {
            coinInfo.value.sellPrice = price
        }
    }
    const getPrice = (coin: string) => {
        return coinPriceList.value.find(item => item.coinName === coin)
    }
    const logoList = ref<{}>({})
    const setLogoList = (list: {}) => {
        logoList.value = list
    }
    const infoMA = ref({})
    const infoEMA = ref({})
    const infoVOL = ref({})
    const infoMACD = ref({})
    const infoPrice = ref({
        close: '',
        high: '',
        low: '',
        open: '',
        time: '',
        volume: ''
    })
    const setMA = (ma: {}) => {
        infoMA.value = ma
    }
    const setEMA = (ema: {}) => {
        infoEMA.value = ema
    }
    const setVOL = (vol: {}) => {
        infoVOL.value = vol
    }
    const setMACD = (macd: {}) => {
        infoMACD.value = macd
    }
    const setInfoPrice = (newInfoPrice: {
        close: string;
        high: string;
        low: string;
        open: string;
        time: string;
        volume: string;
    }) => {
        infoPrice.value = newInfoPrice
    }
    const setInCharts = (inCharts: boolean) => {
        coinInfo.value.inCharts = inCharts
    }
    const setCoinName = (name: string) => {
        coinInfo.value.name = name
    }
    const setCoinPrice = (price: string) => {
        coinInfo.value.price = price
    }
    const setCoinKlineType = (kLineType: string) => {
        coinInfo.value.kLineType = kLineType
    }
    return { coinInfo, contractKLine, setContractKLine, spotKLine, setSpotKLine, setCoinName, setMACD, infoMACD, infoVOL, setVOL, setInCharts, setCoinPrice, setCoinKlineType, setPirce, logoList, setLogoList, infoMA, setMA, setInfoPrice, infoPrice, infoEMA, setEMA, getPrice, coinPriceList, setCoinPriceList }
}, {
    persist: {
        storage: {
            getItem(key) {
                return localStorage.getItem(key)
            },
            setItem(key, value) {
                localStorage.setItem(key, value)
            }
        }
    }
})
